package operation

import (
	"context"
	"rdSql/rds"
	"rdSql/table"
	"rdSql/util"
	"strings"
)

// A series of inert operations
type InsertOperation struct {
}

func NewInsertOperation() *InsertOperation {
	return &InsertOperation{}
}

func (i *InsertOperation) InsertOne(data table.RTable) error {
	defaultTable := table.NewDefaultTable(data)
	defaultTable.Parse()
	valueExtra := util.Symbol + strings.Join(defaultTable.TFieldVal, util.Symbol)
	keyPre := defaultTable.GetKeyPre(valueExtra)
	_, err := rds.RdsClient.HMSet(context.Background(), keyPre, defaultTable.ParseVal).Result()
	if err != nil {
		return err
	}
	return nil
}

func (i *InsertOperation) InsertMany(datas []table.RTable) error {
	for _, data := range datas {
		if err := i.InsertOne(data); err != nil {
			return err
		}
	}
	return nil
}

// sync insert
func (i InsertOperation) InsertManySync(data []table.TFields) {

}
